package com.hxzy.controller;

import com.hxzy.pojo.Result;
import com.hxzy.utils.AliOssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author wzm
 * @create 2023-10-10 10:21
 * project_name:stu_sys
 */
//测试文件上传的控制器
@Slf4j
@RestController
public class UploadController {
    @Autowired
    AliOssUtils aliOssUtils;
    //本地
  /*  @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("文件上传的表单数据：{},{},{}",username,age,image);
        //将临时数据转移到磁盘中保存
        //获取原始的文件名 7.8.aaa.txt
        String originalFilename = image.getOriginalFilename();

        //对文件名进行编辑防止上传文件被覆盖  UUID+后缀
        String newFileName= UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));


        //将文件存储在服务器所在的磁盘目录
        image.transferTo(new File("E:\\images\\"+newFileName));
        return Result.success();
    }*/

    //阿里云oss
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传数据：,{}",image);
        //上传图片到阿里云oss 工具
        String upload = aliOssUtils.upload(image);
        //返回url到前端
        return Result.success(upload);
    }
}
